home *** CD-ROM | disk | FTP | other *** search
/ Aminet 40 / Aminet 40 (2000)(Schatztruhe)[!][Dec 2000].iso / Aminet / game / shoot / ADoomPPC_src.lha / ADoomPPC_src / m_misc.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-14  |  11.7 KB  |  610 lines

  1. // Emacs style mode select   -*- C++ -*- 
  2. //-----------------------------------------------------------------------------
  3. //
  4. // $Id:$
  5. //
  6. // Copyright (C) 1993-1996 by id Software, Inc.
  7. //
  8. // This source is available for distribution and/or modification
  9. // only under the terms of the DOOM Source Code License as
  10. // published by id Software. All rights reserved.
  11. //
  12. // The source is distributed in the hope that it will be useful,
  13. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. // FITNESS FOR A PARTICULAR PURPOSE. See the DOOM Source Code License
  15. // for more details.
  16. //
  17. //
  18. // $Log:$
  19. //
  20. // DESCRIPTION:
  21. //    Main loop menu stuff.
  22. //    Default Config File.
  23. //    PCX Screenshots.
  24. //
  25. //-----------------------------------------------------------------------------
  26.  
  27. static const char
  28. rcsid[] = "$Id: m_misc.c,v 1.6 1997/02/03 22:45:10 b1 Exp $";
  29.  
  30. #ifndef __VBCC__
  31. #ifdef __SASC
  32. #include <stat.h>
  33. #else
  34. #include <sys/stat.h>
  35. #endif
  36. #include <sys/types.h>
  37. #include <fcntl.h>
  38. #include <unistd.h>
  39. #endif
  40. #include <stdlib.h>
  41.  
  42. #include <ctype.h>
  43.  
  44.  
  45. #include "doomdef.h"
  46.  
  47. #include "z_zone.h"
  48.  
  49. #include "m_swap.h"
  50. #include "m_argv.h"
  51.  
  52. #include "w_wad.h"
  53.  
  54. #include "i_system.h"
  55. #include "i_video.h"
  56. #include "v_video.h"
  57.  
  58. #include "hu_stuff.h"
  59.  
  60. // State.
  61. #include "doomstat.h"
  62.  
  63. // Data.
  64. #include "dstrings.h"
  65.  
  66. #include "m_misc.h"
  67.  
  68. //
  69. // M_DrawText
  70. // Returns the final X coordinate
  71. // HU_Init must have been called to init the font
  72. //
  73. extern patch_t*        hu_font[HU_FONTSIZE];
  74.  
  75. int
  76. M_DrawText
  77. ( int        x,
  78.   int        y,
  79.   boolean    direct,
  80.   char*        string )
  81. {
  82.     int     c;
  83.     int        w;
  84.  
  85.     while (*string)
  86.     {
  87.     c = toupper(*string) - HU_FONTSTART;
  88.     string++;
  89.     if (c < 0 || c> HU_FONTSIZE)
  90.     {
  91.         x += 4;
  92.         continue;
  93.     }
  94.         
  95.     w = SWAPSHORT(hu_font[c]->width);
  96.     if (x+w > SCREENWIDTH)
  97.         break;
  98.     if (direct)
  99.         V_DrawPatchDirect(x, y, 0, hu_font[c]);
  100.     else
  101.         V_DrawPatch(x, y, 0, hu_font[c]);
  102.     x+=w;
  103.     }
  104.  
  105.     return x;
  106. }
  107.  
  108.  
  109.  
  110.  
  111. //
  112. // M_WriteFile
  113. //
  114. #ifndef O_BINARY
  115. #define O_BINARY 0
  116. #endif
  117.  
  118. #ifdef __VBCC__
  119. boolean
  120. M_WriteFile
  121. ( char const* name,
  122.     void* source,
  123.     int length )
  124. {
  125.         FILE *handle;
  126.         int count;
  127.  
  128.         handle = fopen (name, "wb");
  129.  
  130.         if (handle == NULL)
  131.             return false;
  132.  
  133.         count = fwrite (source, 1, length, handle);
  134.         fclose (handle);
  135.  
  136.         if (count < length)
  137.             return false;
  138.  
  139.         return true;
  140. }
  141. #else
  142. boolean
  143. M_WriteFile
  144. ( char const*    name,
  145.   void*        source,
  146.   int        length )
  147. {
  148.     int        handle;
  149.     int        count;
  150.     
  151.     handle = open ( name, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666);
  152.  
  153.     if (handle == -1)
  154.     return false;
  155.  
  156.     count = write (handle, source, length);
  157.     close (handle);
  158.     
  159.     if (count < length)
  160.     return false;
  161.         
  162.     return true;
  163. }
  164. #endif
  165.  
  166. //
  167. // M_ReadFile
  168. //
  169. #ifdef __VBCC__
  170.  
  171. int
  172. M_ReadFile
  173. ( char const* name,
  174.     byte** buffer )
  175. {
  176.         FILE *handle;
  177.         int count, length;
  178.         byte *buf;
  179.  
  180.         if ((handle = fopen (name, "r")) == NULL)
  181.             I_Error ("Couldn't read file %s", name);
  182.         fseek (handle, 0, SEEK_END);
  183.         length = ftell (handle);
  184.         fseek (handle, 0, SEEK_SET);
  185.         buf = Z_Malloc (length, PU_STATIC, NULL);
  186.         count = fread (buf, 1, length, handle);
  187.         fclose (handle);
  188.         if (count < length)
  189.             I_Error ("Couldn't read file");
  190.  
  191.         *buffer = buf;
  192.         return length;
  193. }
  194. #else
  195.  
  196. int
  197. M_ReadFile
  198. ( char const*    name,
  199.   byte**    buffer )
  200. {
  201.     FILE *handle;
  202.     int    count, length;
  203.     struct stat    *fileinfo = NULL;
  204.     byte        *buf;
  205.  
  206.     if ((handle = fopen (name, "r")) == NULL ||
  207.         (fileinfo = malloc (sizeof(struct stat))) == NULL ||
  208.         fstat (fileno(handle), fileinfo) == -1)
  209.         I_Error ("Couldn't read file %s", name);
  210.     length = fileinfo->st_size;
  211.     free (fileinfo);
  212.     buf = Z_Malloc (length, PU_STATIC, NULL);
  213.     count = fread (buf, 1, length, handle);
  214.     fclose (handle);
  215. /*
  216.     handle = open (name, O_RDONLY | O_BINARY, 0666);
  217.     if (handle == -1)
  218.     I_Error ("Couldn't read file %s", name);
  219.     if (fstat (handle,&fileinfo) == -1)
  220.     I_Error ("Couldn't read file %s", name);
  221.     length = fileinfo.st_size;
  222.     buf = Z_Malloc (length, PU_STATIC, NULL);
  223.     count = read (handle, buf, length);
  224.     close (handle);
  225. */
  226.     if (count < length)
  227.     I_Error ("Couldn't read file %s", name);
  228.  
  229.     *buffer = buf;
  230.     return length;
  231. }
  232. #endif
  233.  
  234. //
  235. // DEFAULTS
  236. //
  237. int        usemouse;
  238. int        usejoystick;
  239.  
  240. extern int    key_right;
  241. extern int    key_left;
  242. extern int    key_up;
  243. extern int    key_down;
  244.  
  245. extern int    key_strafeleft;
  246. extern int    key_straferight;
  247.  
  248. extern int    key_fire;
  249. extern int    key_use;
  250. extern int    key_strafe;
  251. extern int    key_speed;
  252.  
  253. extern int    mousebfire;
  254. extern int    mousebstrafe;
  255. extern int    mousebforward;
  256.  
  257. extern int    joybfire;
  258. extern int    joybstrafe;
  259. extern int    joybuse;
  260. extern int    joybspeed;
  261.  
  262. extern int    viewwidth;
  263. extern int    viewheight;
  264.  
  265. extern int    mouseSensitivity;
  266. extern int    showMessages;
  267.  
  268. extern int    detailLevel;
  269.  
  270. extern int    screenblocks;
  271.  
  272. extern int    showMessages;
  273.  
  274. // machine-independent sound params
  275. extern    int    numChannels;
  276.  
  277.  
  278. // UNIX hack, to be removed.
  279. #ifdef SNDSERV
  280. extern char*    sndserver_filename;
  281. extern int    mb_used;
  282. #endif
  283.  
  284. #ifdef LINUX
  285. char*        mousetype;
  286. char*        mousedev;
  287. #endif
  288.  
  289. extern char*    chat_macros[];
  290.  
  291.  
  292.  
  293. typedef struct
  294. {
  295.     char*    name;
  296.     int*    location;
  297.     int        defaultvalue;
  298.     int        scantranslate;        // PC scan code hack
  299.     int        untranslated;        // lousy hack
  300. } default_t;
  301.  
  302. default_t    defaults[] =
  303. {
  304.     {"mouse_sensitivity",&mouseSensitivity, 5},
  305.     {"sfx_volume",&snd_SfxVolume, 8},
  306.     {"music_volume",&snd_MusicVolume, 8},
  307.     {"show_messages",&showMessages, 1},
  308.     
  309.  
  310. #ifdef NORMALUNIX
  311.     {"key_right",&key_right, KEY_RIGHTARROW},
  312.     {"key_left",&key_left, KEY_LEFTARROW},
  313.     {"key_up",&key_up, KEY_UPARROW},
  314.     {"key_down",&key_down, KEY_DOWNARROW},
  315.     {"key_strafeleft",&key_strafeleft, ','},
  316.     {"key_straferight",&key_straferight, '.'},
  317.  
  318.     {"key_fire",&key_fire, KEY_RCTRL},
  319.     {"key_use",&key_use, ' '},
  320.     {"key_strafe",&key_strafe, KEY_RALT},
  321.     {"key_speed",&key_speed, KEY_RSHIFT},
  322.  
  323. // UNIX hack, to be removed. 
  324. #ifdef SNDSERV
  325.     {"sndserver", (int *) &sndserver_filename, (int) "sndserver"},
  326.     {"mb_used", &mb_used, 2},
  327. #endif
  328.     
  329. #endif
  330.  
  331. #ifdef LINUX
  332.     {"mousedev", (int*)&mousedev, (int)"/dev/ttyS0"},
  333.     {"mousetype", (int*)&mousetype, (int)"microsoft"},
  334. #endif
  335.  
  336.     {"use_mouse",&usemouse, 1},
  337.     {"mouseb_fire",&mousebfire,0},
  338.     {"mouseb_strafe",&mousebstrafe,1},
  339.     {"mouseb_forward",&mousebforward,2},
  340.  
  341. #ifdef AMIGA
  342.     {"use_joystick",&usejoystick, 1},
  343. #else
  344.     {"use_joystick",&usejoystick, 0},
  345. #endif
  346.     {"joyb_fire",&joybfire,0},
  347.     {"joyb_strafe",&joybstrafe,1},
  348.     {"joyb_use",&joybuse,3},
  349.     {"joyb_speed",&joybspeed,2},
  350.  
  351.     {"screenblocks",&screenblocks, 9},
  352.     {"detaillevel",&detailLevel, 0},
  353.  
  354. #ifdef AMIGA
  355.     {"snd_channels",&numChannels, 2},   /* 2 for effects + 2 for music */
  356. #else
  357.     {"snd_channels",&numChannels, 3},
  358. #endif
  359.  
  360.  
  361.     {"usegamma",&usegamma, 0},
  362.  
  363.     {"chatmacro0", (int *) &chat_macros[0], (int) HUSTR_CHATMACRO0 },
  364.     {"chatmacro1", (int *) &chat_macros[1], (int) HUSTR_CHATMACRO1 },
  365.     {"chatmacro2", (int *) &chat_macros[2], (int) HUSTR_CHATMACRO2 },
  366.     {"chatmacro3", (int *) &chat_macros[3], (int) HUSTR_CHATMACRO3 },
  367.     {"chatmacro4", (int *) &chat_macros[4], (int) HUSTR_CHATMACRO4 },
  368.     {"chatmacro5", (int *) &chat_macros[5], (int) HUSTR_CHATMACRO5 },
  369.     {"chatmacro6", (int *) &chat_macros[6], (int) HUSTR_CHATMACRO6 },
  370.     {"chatmacro7", (int *) &chat_macros[7], (int) HUSTR_CHATMACRO7 },
  371.     {"chatmacro8", (int *) &chat_macros[8], (int) HUSTR_CHATMACRO8 },
  372.     {"chatmacro9", (int *) &chat_macros[9], (int) HUSTR_CHATMACRO9 }
  373.  
  374. };
  375.  
  376. int    numdefaults;
  377. char*    defaultfile;
  378.  
  379.  
  380. //
  381. // M_SaveDefaults
  382. //
  383. void M_SaveDefaults (void)
  384. {
  385.     int        i;
  386.     int        v;
  387.     FILE*    f;
  388.     
  389.     f = fopen (defaultfile, "w");
  390.     if (!f)
  391.     return; // can't write the file, but don't complain
  392.         
  393.     for (i=0 ; i<numdefaults ; i++)
  394.     {
  395.     if (defaults[i].defaultvalue > -0xfff
  396.         && defaults[i].defaultvalue < 0xfff)
  397.     {
  398.         v = *defaults[i].location;
  399.         fprintf (f,"%s\t\t%i\n",defaults[i].name,v);
  400.     } else {
  401.         fprintf (f,"%s\t\t\"%s\"\n",defaults[i].name,
  402.              * (char **) (defaults[i].location));
  403.     }
  404.     }
  405.     
  406.     fclose (f);
  407. }
  408.  
  409.  
  410. //
  411. // M_LoadDefaults
  412. //
  413. extern byte    scantokey[128];
  414.  
  415. void M_LoadDefaults (void)
  416. {
  417.     int        i;
  418.     int        len;
  419.     FILE*    f;
  420.     char    def[80];
  421.     char    strparm[100];
  422.     char*    newstring = NULL;
  423.     int        parm;
  424.     boolean    isstring;
  425.     
  426.     // set everything to base values
  427.     numdefaults = sizeof(defaults)/sizeof(defaults[0]);
  428.     for (i=0 ; i<numdefaults ; i++)
  429.     *defaults[i].location = defaults[i].defaultvalue;
  430.     
  431.     // check for a custom default file
  432.     i = M_CheckParm ("-config");
  433.     if (i && i<myargc-1)
  434.     {
  435.     defaultfile = myargv[i+1];
  436.     printf ("    default file: %s\n",defaultfile);
  437.     }
  438.     else
  439.     defaultfile = basedefault;
  440.     
  441.     // read the file in, overriding any set defaults
  442.     f = fopen (defaultfile, "r");
  443.     if (f)
  444.     {
  445.     while (!feof(f))
  446.     {
  447.         isstring = false;
  448.         if (fscanf (f, "%79s %[^\n]\n", def, strparm) == 2)
  449.         {
  450.         if (strparm[0] == '"')
  451.         {
  452.             // get a string default
  453.             isstring = true;
  454.             len = strlen(strparm);
  455.             newstring = (char *) malloc(len);
  456.             strparm[len-1] = 0;
  457.             strcpy(newstring, strparm+1);
  458.         }
  459.         else if (strparm[0] == '0' && strparm[1] == 'x')
  460.             sscanf(strparm+2, "%x", &parm);
  461.         else
  462.             sscanf(strparm, "%i", &parm);
  463.         for (i=0 ; i<numdefaults ; i++)
  464.             if (!strcmp(def, defaults[i].name))
  465.             {
  466.             if (!isstring)
  467.                 *defaults[i].location = parm;
  468.             else
  469.                 *defaults[i].location =
  470.                 (int) newstring;
  471.             break;
  472.             }
  473.         }
  474.     }
  475.         
  476.     fclose (f);
  477.     }
  478. }
  479.  
  480.  
  481. //
  482. // SCREEN SHOTS
  483. //
  484.  
  485.  
  486. typedef struct
  487. {
  488.     char        manufacturer;
  489.     char        version;
  490.     char        encoding;
  491.     char        bits_per_pixel;
  492.  
  493.     unsigned short    xmin;
  494.     unsigned short    ymin;
  495.     unsigned short    xmax;
  496.     unsigned short    ymax;
  497.     
  498.     unsigned short    hres;
  499.     unsigned short    vres;
  500.  
  501.     unsigned char    palette[48];
  502.     
  503.     char        reserved;
  504.     char        color_planes;
  505.     unsigned short    bytes_per_line;
  506.     unsigned short    palette_type;
  507.     
  508.     char        filler[58];
  509.     unsigned char    data;        // unbounded
  510. } pcx_t;
  511.  
  512.  
  513. //
  514. // WritePCXfile
  515. //
  516. void
  517. WritePCXfile
  518. ( char*        filename,
  519.   byte*        data,
  520.   int        width,
  521.   int        height,
  522.   byte*        palette )
  523. {
  524.     int        i;
  525.     int        length;
  526.     pcx_t*    pcx;
  527.     byte*    pack;
  528.  
  529.     pcx = Z_Malloc (width*height*2+1000, PU_STATIC, NULL);
  530.  
  531.     pcx->manufacturer = 0x0a;        // PCX id
  532.     pcx->version = 5;            // PaintBrush v3.0
  533.     pcx->encoding = 1;            // uncompressed
  534.     pcx->bits_per_pixel = 8;        // 256 color
  535.     pcx->xmin = 0;
  536.     pcx->ymin = 0;
  537.     pcx->xmax = SWAPSHORT(width-1);
  538.     pcx->ymax = SWAPSHORT(height-1);
  539.     pcx->hres = SWAPSHORT(width);
  540.     pcx->vres = SWAPSHORT(height);
  541.     memset (pcx->palette,0,sizeof(pcx->palette));
  542.     pcx->color_planes = 1;        // chunky image
  543.     pcx->bytes_per_line = SWAPSHORT(width);
  544.     pcx->palette_type = SWAPSHORT(1);    // color information
  545.     memset (pcx->filler,0,sizeof(pcx->filler));
  546.  
  547.  
  548.     // pack the image
  549.     pack = &pcx->data;
  550.     
  551.     for (i=0 ; i<width*height ; i++)
  552.     {
  553.     if ( (*data & 0xc0) != 0xc0)
  554.         *pack++ = *data++;
  555.     else
  556.     {
  557.         *pack++ = 0xc1;
  558.         *pack++ = *data++;
  559.     }
  560.     }
  561.     
  562.     // write the palette
  563.     *pack++ = 0x0c;    // palette ID byte
  564.     for (i=0 ; i<768 ; i++)
  565.     *pack++ = *palette++;
  566.     
  567.     // write output file
  568.     length = pack - (byte *)pcx;
  569.     M_WriteFile (filename, pcx, length);
  570.  
  571.     Z_Free (pcx);
  572. }
  573.  
  574.  
  575. //
  576. // M_ScreenShot
  577. //
  578. void M_ScreenShot (void)
  579. {
  580.     int        i;
  581.     byte*    linear;
  582.     char    lbmname[12];
  583.     
  584.     // munge planar buffer to linear
  585.     linear = screens[2];
  586.     I_ReadScreen (linear);
  587.     
  588.     // find a file name to save it to
  589.     strcpy(lbmname,"DOOM00.pcx");
  590.         
  591.     for (i=0 ; i<=99 ; i++)
  592.     {
  593.     lbmname[4] = i/10 + '0';
  594.     lbmname[5] = i%10 + '0';
  595.     if (access(lbmname,0) == -1)
  596.         break;    // file doesn't exist
  597.     }
  598.     if (i==100)
  599.     I_Error ("M_ScreenShot: Couldn't create a PCX");
  600.     
  601.     // save the pcx file
  602.     WritePCXfile (lbmname, linear,
  603.           SCREENWIDTH, SCREENHEIGHT,
  604.           W_CacheLumpName ("PLAYPAL",PU_CACHE));
  605.     
  606.     players[consoleplayer].message = "screen shot";
  607. }
  608.  
  609.  
  610.